/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BADebugServerSelectorViewController.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 30/01/14.
//  Copyright (c) 2014 redia. All rights reserved.
//

#import "BADebugServerSelectorViewController.h"
#import "BARPCManager.h"


@interface BADebugServerSelectorViewController ()


@property (weak, nonatomic) IBOutlet UIView *boxView;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *prodButton;
@property (weak, nonatomic) IBOutlet UIButton *devButton;

@end

@implementation BADebugServerSelectorViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.boxView.layer.cornerRadius = 5;
    
    
}

-(void)updateInfo
{
    if ([BARPCManager instance].isUsingDevServer) {
        self.infoLabel.text = @"Currently using development server";
    } else {
        self.infoLabel.text = @"Currently using production server";
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dismiss
{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (IBAction)cancelClicked:(id)sender {
    [self dismiss];
}

- (IBAction)prodClicked:(id)sender {
    [BARPCManager instance].isUsingDevServer = NO;
    [self updateInfo];

    [[NSNotificationCenter defaultCenter] postNotificationName:BA_DEBUG_SERVER_SELECTOR_RELOAD_LIBRARY_LIST object:nil];
    [self dismiss];

}

- (IBAction)devClicked:(id)sender {
    [BARPCManager instance].isUsingDevServer = YES;
    [self updateInfo];

    [[NSNotificationCenter defaultCenter] postNotificationName:BA_DEBUG_SERVER_SELECTOR_RELOAD_LIBRARY_LIST object:nil];
    [self dismiss];

}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString* newstring = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if ([newstring isEqualToString:@"caxYQops"]) {
        self.prodButton.hidden=NO;
        self.devButton.hidden=NO;
        self.infoLabel.hidden=NO;
        [self updateInfo];
    } else {
        self.prodButton.hidden=YES;
        self.devButton.hidden=YES;
        self.infoLabel.hidden=YES;
   
    }
    
    return YES;
}


@end
